<?php
/**
 * common model file Created by PhpStorm.
 * User: yang
 * Date: 2020/06/15
 */

namespace Common\Model;

class Forum extends Base
{
    protected $table = 'forum';
    protected $guarded = [];

    //public $timestamps = false;

    //一对一关联用户
    public function get_user()
    {
        return $this->hasOne(User::class, 'id', 'user_id')
            ->select('id', 'name', 'rank', 'head_img', 'sign');
    }

    //一对一关联分类
    public function get_type()
    {
        return $this->hasOne(ForumType::class, 'id', 'type_id')
            ->select('id', 'title');
    }

    //一对多关联评论
    public function get_comment()
    {
        return $this->hasMany(ForumComment::class, 'forum_id', 'id')
            ->with(['get_user']);
    }

    //一对多关联点赞
    public function get_zan()
    {
        return $this->hasMany(ForumZan::class, 'forum_id', 'id')
            ->where('type', 1)
            ->select(['id', 'forum_id', 'user_id']);
    }

    /**
     * 读取数据处理
     * @param $value
     * @return string
     */
    public function getTagAttribute($value)
    {
        switch ($value) {
            case 1:
                $result = '<span class="layui-badge layui-bg-gray">粗品</span>';
                break;
            case 2:
                $result = '<span class="layui-badge fly-badge-accept">一般</span>';
                break;
            case 3:
                $result = '<span class="layui-badge layui-bg-blue">上品</span>';
                break;
            case 4:
                $result = '<span class="layui-badge layui-bg-orange">精品</span>';
                break;
            default:
                $result = '&nbsp';
        }
        return $result;
    }
}